package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.adapter.ExpandablelistAdapter;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.main.HomeActivityGroup;
import com.kuanter.kuanterauto.utils.Animations;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
import com.kuanter.kuanterauto.view.CustomExpandableListView;
import com.kuanter.kuanterauto.view.CustomLetterView;
import com.kuanter.kuanterauto.view.CustomLetterView.OnTouchingLetterChangedListener;

/**
 * 城市列表
 * 
 * @author kuanter
 * 
 */
@Deprecated
public class CitysActivity extends Activity implements
		OnChildClickListener, OnGroupCollapseListener,
		OnTouchingLetterChangedListener {

	// 城市
	private String[] PINPAI_STRS_B; //{ "北京市"}

	private String[] PINPAI_STRS_C;//{ "重庆市", "长沙市", "成都市"};

	private String[] PINPAI_STRS_G;//{ "广州市"};

	private String[] PINPAI_STRS_J;//{ "济南市"};

	private String[] PINPAI_STRS_N;//{ "南京市" };

	private String[] PINPAI_STRS_S;//{ "上海市"};


	private String[] PINPAI_STRS_W;//{ "武汉市"};

	private String[] PINPAI_STRS_X;//{ "西安市"};


	private String[] PINPAI_STRS_Z;//{ "郑州市"};

	private final String[] ZIMU = {"B", "C", "G", "J",
			"N",  "S",  "W", "X", "Z" };

	private CustomExpandableListView expandable;
	private CustomLetterView letter_view;

	private ArrayList<String> groupList = new ArrayList<String>();
	private ArrayList<ArrayList<String>> childList = new ArrayList<ArrayList<String>>();
	private ArrayList<ArrayList<Integer>> childList_IMG = new ArrayList<ArrayList<Integer>>();
	private ExpandablelistAdapter testExpandablelistAdapter;

//	public static  RelativeLayout rl_childxh;
	public static ImageView iv_yy_xxpp=null;
	private Button btTopLeft_xzpp=null;
	private Animations a = null;
	 int i = 0;
	 
	 TextView current_city_text;
	 TextView current_city;
	 LinearLayout progress_location;
	 ImageButton refresh_city;
	 
	 String city = "";
	 
	 public static int viewOrVisiable = -1;  //-1表示刷新，1表示显示
	 
	 private boolean back = false;  //很多界面调用城市列表,首次进入时不需要返回信息而跳到主界面,从主界面跳转到城市列表时用forResult跳转，这是需要专递参数back=true以做判断
	 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.kuanter_citys);
		
		initCitys();
		
		//欢迎画面后跳转到城市列表
		Intent intent = getIntent();
		if(intent != null) {
			back = intent.getBooleanExtra("back", false);
		}
//		Bundle bundle = intent.getExtras();
//		if(bundle != null) {
//			city = bundle.getString("city");
//		}
		//从配置中获取city
		city = SharePrefenceUtil.getCity(this);

//		initChildXH();
		initGroupList();
		initChildList();

		initView();
		setListener();
	}

	private void initCitys() {
		PINPAI_STRS_B = getResources().getStringArray(R.array.city_b);
		PINPAI_STRS_C = getResources().getStringArray(R.array.city_c);
		PINPAI_STRS_G = getResources().getStringArray(R.array.city_g);
		PINPAI_STRS_J = getResources().getStringArray(R.array.city_j);
		PINPAI_STRS_N = getResources().getStringArray(R.array.city_n);
		PINPAI_STRS_S = getResources().getStringArray(R.array.city_s);
		PINPAI_STRS_W = getResources().getStringArray(R.array.city_w);
		PINPAI_STRS_X = getResources().getStringArray(R.array.city_x);
		PINPAI_STRS_Z = getResources().getStringArray(R.array.city_z);
	}

	private void initView() {
		current_city_text = (TextView) findViewById(R.id.current_city_text);
		current_city = (TextView) findViewById(R.id.current_city);
		progress_location = (LinearLayout) findViewById(R.id.progress_location);
		refresh_city = (ImageButton) findViewById(R.id.refresh_city);
		refresh_city.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				viewOrVisiable = -1;
				changeViewStatus();
				ProApplication application = (ProApplication) getApplication();
				application.startRequest(mHandler);
			}
		});
		
		//进入本页面默认是progress显示，若城市已设置则更变状态
		if(!city.equals("")) {
			viewOrVisiable = 1;
			changeViewStatus();
		} else {
			//没有设置城市,调用applicaiotn的请求定位,true表示把请求得到的数据写入配置文件，第二个参数为了区别刷新，因为刷新位置时不需要写如配置
			ProApplication application = (ProApplication) getApplication();
			application.startRequest(mHandler,true);
		}
		
		a = new Animations(this);
		btTopLeft_xzpp=(Button) findViewById(R.id.btTopLeft_xzpp);
		iv_yy_xxpp=(ImageView) findViewById(R.id.iv_yy_xxpp);
		
		expandable = (CustomExpandableListView) findViewById(R.id.expandable);
		expandable.setHeaderView(getLayoutInflater().inflate(
				R.layout.expandablelist_group_item, expandable, false));
		letter_view = (CustomLetterView) findViewById(R.id.letter_view);

		

//		i = rl_childxh.getVisibility();

		// 设置adapter并且全部展开
		testExpandablelistAdapter = new ExpandablelistAdapter(this,
				expandable, groupList, childList, childList_IMG,false);
		expandable.setAdapter(testExpandablelistAdapter);
		int groupSize = groupList.size();
		for (int i = 0; i < groupSize; i++) {
			expandable.expandGroup(i);
		}

		// 获取密度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		letter_view.setList(groupList, dm);
		letter_view.invalidate();
	}

	/**
	 * 设置监听
	 */
	private void setListener() {
		letter_view.setOnTouchingLetterChangedListener(this);
		expandable.setOnChildClickListener(this);
		expandable.setOnGroupCollapseListener(this);
		expandable.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return false;
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}

	/**
	 * 右边字母表的排列事件
	 */
	@Override
	public void onTouchingLetterChanged(int groupPosition) {
		expandable.setSelectedGroup(groupPosition);
		
		
//		letter_view.invalidate();
	}

	/**
	 * 点击child事件
	 */
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		System.out.println("groupPosition=" + groupPosition + "    "
				+ "childPosition=" + childPosition);
		TextView tv = (TextView) v.findViewById(R.id.tv_pp);
		String city = tv.getText().toString();
		SharePrefenceUtil.setCity(CitysActivity.this, city);
//		Toast.makeText(XgcxActivity.this, text, 3000).show();
		if(!back) {
			Intent intent=new Intent(CitysActivity.this,HomeActivityGroup.class);
			startActivity(intent);
		} else {
			Intent intent = new Intent();
			intent.putExtra("city", city);
			setResult(200, intent);
		}

		finish();
		
		return false;
	}
	
	
	/**
	 *  
	 */
	@Override
	public void onGroupCollapse(int groupPosition) {
		if (expandable.isGroupExpanded(groupPosition)) {
			return;
		} else {
			expandable.expandGroup(groupPosition);
		}
	}

	private void initGroupList() {
		for (int i = 0; i < ZIMU.length; i++) {
			groupList.add(ZIMU[i]);
		}
	}

	private void initChildList() {
		ArrayList<String> B = new ArrayList<String>();
		for (int i = 0; i < PINPAI_STRS_B.length; i++) {
			B.add(PINPAI_STRS_B[i]);
		}
		ArrayList<String> C = new ArrayList<String>();
		for (int i = 0; i < PINPAI_STRS_C.length; i++) {
			C.add(PINPAI_STRS_C[i]);
		}
		
		
		ArrayList<String> G = new ArrayList<String>();
		for (int i = 0; i < PINPAI_STRS_G.length; i++) {
			G.add(PINPAI_STRS_G[i]);
		}
		
		ArrayList<String> J = new ArrayList<String>();
		for (int i = 0; i < PINPAI_STRS_J.length; i++) {
			J.add(PINPAI_STRS_J[i]);
		}
		
		
		ArrayList<String> N = new ArrayList<String>();
		for (int i = 0; i < PINPAI_STRS_N.length; i++) {
			N.add(PINPAI_STRS_N[i]);
		}
		
		ArrayList<String> S = new ArrayList<String>();
		for (int i = 0; i < PINPAI_STRS_S.length; i++) {
			S.add(PINPAI_STRS_S[i]);
		}
		
		ArrayList<String> W = new ArrayList<String>();
		for (int i = 0; i < PINPAI_STRS_W.length; i++) {
			W.add(PINPAI_STRS_W[i]);
		}
		ArrayList<String> X = new ArrayList<String>();
		for (int i = 0; i < PINPAI_STRS_X.length; i++) {
			X.add(PINPAI_STRS_X[i]);
		}
		
		ArrayList<String> Z = new ArrayList<String>();
		for (int i = 0; i < PINPAI_STRS_Z.length; i++) {
			Z.add(PINPAI_STRS_Z[i]);
		}

		childList.add(B);
		childList.add(C);
		childList.add(G);
		childList.add(J);
		childList.add(N);
		childList.add(S);
		childList.add(W);
		childList.add(X);
		childList.add(Z);
	}
	
	public void exit(View view) {
		finish();
	}
	
	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if(msg.what == ProApplication.location_cuccess) {
				city = (String) msg.obj;
				viewOrVisiable = 1;
				changeViewStatus();
			}
			super.handleMessage(msg);
		}
		
	};
	
	//根据状态改变是否显示progressbar
	public void changeViewStatus() {
		if(viewOrVisiable == -1) {
			current_city_text.setVisibility(View.GONE);
			current_city.setVisibility(View.GONE);
			current_city.setText("");
			progress_location.setVisibility(View.VISIBLE);
			refresh_city.setVisibility(View.GONE);

			
		} else if(viewOrVisiable == 1) {
			
			current_city_text.setVisibility(View.VISIBLE);
			current_city.setVisibility(View.VISIBLE);
			current_city.setText(city);
			progress_location.setVisibility(View.GONE);
			refresh_city.setVisibility(View.VISIBLE);
		}
	}
	
}
